package spring.boot.adminx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import spring.boot.adminx.model.SysConfig;
import spring.boot.adminx.service.SysConfigService;
import spring.boot.adminx.utils.PageUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/sys/config")
public class SysConfigController {

    @Autowired
    private SysConfigService sysConfigService;


    /** 分页查询 */
    @GetMapping("list")
    @PreAuthorize("hasRole('SYS_CONFIG_LIST')")
    public String list(Model model, Integer pageNum, Integer pageSize, String content){
        if (pageNum==null){
            pageNum = 1;
        }
        if (pageSize==null){
            pageSize = PageUtils.PAGE_SIZE;
        }
        Page<SysConfig> page = new Page<>(pageNum,pageSize);
        QueryWrapper<SysConfig> wrapper = new QueryWrapper<>();

        if (content!=null&&content.trim().length()>0){
            model.addAttribute("content",content);
            wrapper.like("type",content)
                    .or().like("key",content)
                    .or().like("value",content)
                    .or().like("type_remark",content)
                    .or().like("remark",content)
                    .or().like("key_remark",content);
        }

        IPage<SysConfig> sysConfigIPage = sysConfigService.page(page, wrapper);
        model.addAttribute("pages",sysConfigIPage);

        List<Long> beforePages = new ArrayList<>();
        List<Long> afterPages = new ArrayList<>();
        PageUtils.page(sysConfigIPage.getPages(),sysConfigIPage.getCurrent(),beforePages,afterPages);
        model.addAttribute("beforePages",beforePages);
        model.addAttribute("afterPages",afterPages);

        return "system/config/list";
    }


    /** 启用停用 */
    @PostMapping("enable/{id}/{enable}")
    @ResponseBody
    @PreAuthorize("hasRole('SYS_CONFIG_ENABLE')")
    public boolean enable(@PathVariable String id, @PathVariable String enable){
        UpdateWrapper<SysConfig> wrapper = new UpdateWrapper<>();
        wrapper.set("enable",enable);
        wrapper.eq("id",id);
        return sysConfigService.update(wrapper);
    }

    @GetMapping("to/add")
    @PreAuthorize("hasRole('SYS_CONFIG_ADD')")
    public String toAdd(){
        return "system/config/add";
    }

    @PostMapping("insert")
    @ResponseBody
    @PreAuthorize("hasRole('SYS_CONFIG_ADD')")
    public String insert(String type,String typeRemark,String key,String keyRemark,String value,String remark){
        SysConfig sysConfig = new SysConfig();
        sysConfig.setId(IdWorker.getId());
        sysConfig.setType(type);
        sysConfig.setTypeRemark(typeRemark);
        sysConfig.setKey(key);
        sysConfig.setKeyRemark(keyRemark);
        sysConfig.setValue(value);
        sysConfig.setRemark(remark);
        sysConfig.setCreateTime(System.currentTimeMillis());
        sysConfig.setEnable(1);
        if (sysConfigService.save(sysConfig)){
            return "true";
        }else{
            return "保存失败";
        }
    }

    @GetMapping("to/update/{id}")
    @PreAuthorize("hasRole('SYS_CONFIG_UPDATE')")
    public String toUpdate(@PathVariable String id, Model model){
        SysConfig sysConfig = sysConfigService.getById(id);
        model.addAttribute("sysConfig",sysConfig);
        return "system/config/edit";
    }

    @PostMapping("update")
    @ResponseBody
    @PreAuthorize("hasRole('SYS_CONFIG_UPDATE')")
    public String update(String id,String type,String typeRemark,String key,String keyRemark,String value,String remark){
        SysConfig sysConfig = new SysConfig();
        sysConfig.setId(Long.valueOf(id));
        sysConfig.setType(type);
        sysConfig.setTypeRemark(typeRemark);
        sysConfig.setKey(key);
        sysConfig.setKeyRemark(keyRemark);
        sysConfig.setValue(value);
        sysConfig.setRemark(remark);
        if (sysConfigService.updateById(sysConfig)){
            return "true";
        }else{
            return "保存失败";
        }
    }
}
